<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>压缩字符串</title>
</head>
<body>
  <script>
    function fn(chars) {
      let write = 0, read, tcnt = 1; //注意这里从1开始
      for (read = 0; read < chars.length; read++) {
        if (read === chars.length - 1 || chars[read] !== chars[read + 1]) {
          chars[write++] = chars[read];
          if (tcnt > 1) {
            tcnt = tcnt + '';
            for (let s of tcnt) {
              chars[write++] = s
            }
          }
          tcnt = 0
        }
        tcnt++
      }
      return write
    }
    console.log(fn(['a', 'a', 'b', 'b', 'c', 'c', 'c']))
  </script>
</body>
</html>